﻿package mortal.game.view.lotteryDraw
{
    import Message.Game.*;
    import Message.Public.*;
    import com.mui.controls.*;
    import com.mui.events.*;
    import extend.language.*;
    import fl.controls.*;
    import fl.data.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.button.*;
    import mortal.game.view.wizard.*;
    import mortal.mvc.core.*;

    public class lotteryDrawPackageView extends BaseWindow
    {
        private var _packTabData:Array;
        private var _pageSize:int = 49;
        private var _posType:int = 0;
        private var _tabBar:GTabarNew;
        private var _packItemPanel:GTileList;
        private var _getAllBtn:GButton;
        private var _sortBtn:TimeButton;
        private static var _instance:lotteryDrawPackageView;

        public function lotteryDrawPackageView()
        {
            this._packTabData = Language.getArray(90201);
            this.init();
            return;
        }// end function

        private function init() : void
        {
            setSize(345, 430);
            title = Language.getString(90427);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.pushUIToDisposeVec(UIFactory.bg(8, 62, 333, 328, this, ImagesConst.PanelBg));
            this._tabBar = UIFactory.gTabBarNew(2, 32, this._packTabData, 343, 500, 65, 26, this, this.tabBarChangeHandler, "TabButtonNew");
            this._tabBar.selectedIndex = 0;
            this._packItemPanel = UIFactory.tileList(10, 65, 344, 322, this);
            this._packItemPanel.columnWidth = 46;
            this._packItemPanel.rowHeight = 46;
            this._packItemPanel.horizontalGap = -1;
            this._packItemPanel.verticalGap = 0;
            this._packItemPanel.setStyle("cellRenderer", lotteryDrawPackageCellRender);
            this._packItemPanel.isShowVerticalScrollBar = true;
            this._packItemPanel.direction = ScrollBarDirection.VERTICAL;
            this._packItemPanel.verticalScrollPolicy = ScrollPolicy.ON;
            this._packItemPanel.configEventListener(MouseEvent.CLICK, this.onItemsClickHandler);
            this.updateAllItems();
            this._getAllBtn = UIFactory.gButton(Language.getString(90428), 203 - 43, 398 + 3, 82, 27, this);
            this._getAllBtn.toolTipData = Language.getString(90429);
            this._sortBtn = UIFactory.timeButton(Language.getString(90430), 203 + 47, 398 + 3, 82, 27, this, "lotteryDrawPackage_sortBtn");
            this._sortBtn.toolTipData = Language.getString(90431);
            this._sortBtn.cdTime = 10000;
            var _loc_1:* = UIFactory.gTextField(Language.getString(90432), 10, 395, 220, 60, this, GlobalStyle.textFormatLv);
            _loc_1.mouseEnabled = false;
            this.pushUIToDisposeVec(_loc_1);
            this._getAllBtn.addEventListener(MouseEvent.CLICK, this.onClickBtnHandle);
            this._sortBtn.addEventListener(MouseEvent.CLICK, this.onClickBtnHandle);
            return;
        }// end function

        private function transferData(param1:Array) : Array
        {
            var _loc_3:ItemData = null;
            var _loc_4:SBagItem = null;
            var _loc_2:Array = [];
            for each (_loc_3 in param1)
            {
                
                if (_loc_3)
                {
                    _loc_4 = new SBagItem();
                    _loc_4.uid = _loc_3.uid;
                    _loc_4.amount = _loc_3.itemAmount;
                    _loc_2.push(_loc_4);
                }
            }
            return _loc_2;
        }// end function

        private function onClickBtnHandle(event:MouseEvent) : void
        {
            if (event.target == this._getAllBtn)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.LotteryDrawGetReward, null));
            }
            else if (event.target == this._sortBtn)
            {
                this.tityItem();
            }
            return;
        }// end function

        private function tityItem() : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.BackPack_Tity, EPlayerItemPosType._EPlayerItemPosLottery));
            return;
        }// end function

        private function onItemsClickHandler(event:MouseEvent) : void
        {
            var _loc_3:Array = null;
            var _loc_2:* = this._packItemPanel.selectedItem as ItemData;
            if (_loc_2)
            {
                if (Cache.instance.pack.backPackCache.isPackFull(_loc_2.itemCode, _loc_2.itemAmount))
                {
                    return MsgManager.showRollTipsMsg(Language.getString(90433), MsgRollTipsType.msgRollTips3);
                }
                _loc_3 = this.transferData([_loc_2]);
                Dispatcher.dispatchEvent(new DataEvent(EventName.LotteryDrawGetReward, _loc_3));
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._getAllBtn.removeEventListener(MouseEvent.CLICK, this.onClickBtnHandle);
            this._tabBar.dispose(param1);
            this._packItemPanel.dispose(param1);
            this._getAllBtn.dispose(param1);
            this._tabBar = null;
            this._packItemPanel = null;
            this._getAllBtn = null;
            if (this._sortBtn)
            {
                DisplayUtil.removeMe(this._sortBtn);
                this._sortBtn.removeEventListener(MouseEvent.CLICK, this.onClickBtnHandle);
                this._sortBtn.dispose(param1);
                this._sortBtn = null;
            }
            return;
        }// end function

        public function updateAllItems() : void
        {
            this._packItemPanel.dataProvider = this.getDataProvider();
            this._packItemPanel.drawNow();
            return;
        }// end function

        private function getDataProvider() : DataProvider
        {
            var _loc_3:int = 0;
            var _loc_1:* = new DataProvider();
            var _loc_2:* = new Array();
            var _loc_4:* = Cache.instance.pack.lotteryDrawBag;
            switch(this._tabBar.selectedIndex)
            {
                case 0:
                {
                    _loc_2 = _loc_4.getAllItems();
                    break;
                }
                case 1:
                {
                    _loc_2 = _loc_4.getEquipItems();
                    break;
                }
                case 2:
                {
                    _loc_2 = _loc_4.getPropItems();
                    break;
                }
                case 3:
                {
                    _loc_2 = _loc_4.getStuffsItems();
                    break;
                }
                default:
                {
                    break;
                }
            }
            _loc_3 = _loc_2.length;
            var _loc_5:int = 0;
            while (_loc_5 < _loc_3)
            {
                
                _loc_1.addItem(_loc_2[_loc_5]);
                _loc_5++;
            }
            return _loc_1;
        }// end function

        private function tabBarChangeHandler(event:MuiEvent = null) : void
        {
            this.updateAllItems();
            return;
        }// end function

        public static function hasInstance() : Boolean
        {
            return _instance != null;
        }// end function

        public static function get instance() : lotteryDrawPackageView
        {
            if (!_instance)
            {
                _instance = new lotteryDrawPackageView;
            }
            return _instance;
        }// end function

    }
}
